package com.cykj.page03;

import java.util.HashMap;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page02
 * @Author: WWJ
 * @CreateTime: 2025-06-17 00:38
 * @Description: 解析请求消息
 * @Version: 1.0
 */
public class MyHttpRequest {
    /*
     * 属性:
     *     请求方法  get post
     *     URL  127.0.0.1:9090/login?account=zhangsan&password=123
     *          127.0.0.1:9090/123.png
     *          127.0.0.1:9090/login.html
     *
     *      请求模块
     *      协议版本
     *      参数列表==>map
     *      请求头列表 ==》map
     *      请求体
     *
     * 三个方法  解析  请求行 请求头 请求体
     * */

    //请求行
    private String requestLine;
    //请求消息
    private String requestMsg;

    //请求方法
    private String requestMethod;
    //请求URL
    private String requestURI;
    //请求模块
    private String requestModel;
    //协议版本
    private String protocol;
    //请求体
    private String requestBody;
    //请求参数
    private HashMap<String, String> requestParams = new HashMap<>();
    //请求头参数
    private HashMap<String, String> requestHeadParams = new HashMap<>();

    //g构造方法获取请求消息
    public MyHttpRequest(String requestMsg) {
        this.requestMsg = requestMsg;
        //解析消息
        parseRequestMessage();
    }
    //解析请求消息
    private void parseRequestMessage() {
        //1.通过回车换行 切割消息
        String[] splitRequestMsgArray = requestMsg.split("/r/n/");
        //2.给请求行赋值
        requestLine = splitRequestMsgArray[0];

        //切割请求行
        parseRequestLine();

        //获取请求体
        System.out.println("==============================");
        System.out.println(splitRequestMsgArray[splitRequestMsgArray.length - 1]);

        if (requestMethod.equals("POST")){
            //请求体
            requestBody = splitRequestMsgArray[splitRequestMsgArray.length - 1];
            //解析请求体
            parseRequestBody();
        }
        //解析请求头
        parseRequestHead(splitRequestMsgArray);
    }

    //解析请求行
    //GET /login?account=wwj&password=123456 HTTP/1.1
    private void parseRequestLine() {
        //通过空格切割请求行 获取请求方法 URL 协议版本
        String[] splitRequestLineArray = requestLine.split(" ");
        /*splitRequestLineArray[0] = "GET"                 // 请求方法
splitRequestLineArray[1] = "/login?account=wwj&password=123456" // URL
splitRequestLineArray[2] = "HTTP/1.1"            // 协议版本
*/
        //请求方法
        requestMethod = splitRequestLineArray[0];
        //URL
        requestURI = splitRequestLineArray[1];
        //协议版本
        protocol = splitRequestLineArray[2];

        //判断是否为GET请求
        if (requestMethod.equals("GET")){
            //判断有没有问号
            if (requestURI.contains("?")){
                //按照第一个？来切割
                String[] splitRequestURLArray = requestURI.split("\\?", 2);
                //请求模块
                requestModel = splitRequestURLArray[0];
                //按照&切割参数[account=list??i,password=12?3456?]
                String[] split = splitRequestURLArray[1].split("&");

                for (String item : split){
                    String[] value = item.split("=");
                    requestParams.put(value[0],value[1]);
                }
            }else {
                requestModel=requestURI;
            }
        }
    }
    //解析请求头
    private void parseRequestHead(String[] splitRequestMsgArray) {
//        System.out.println("===============================");
        for(int i = 0; i < splitRequestMsgArray.length; i++){
            String[] splitRequestHead = splitRequestMsgArray[i].split(":");
            //有头 没有值 会报错
            if (splitRequestHead.length >1){
                requestHeadParams.put(splitRequestHead[0],splitRequestHead[1]);
            }
        }
//        System.out.println(requestHeadParams);
    }

    //post请求才有请求体
    private void parseRequestBody() {
        String[] split = requestBody.split("&");
        for (String item : split){
            String[] splitItem = item.split("=");
            requestHeadParams.put(splitItem[0],splitItem[1]);
        }
        System.out.println(requestParams);
    }

    //根据key 获取value  用于动态资源处理
    public String getRequestParamToKey(String key) {
        return requestParams.get(key);
    }

    public String getProtocol() {
        return protocol;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public HashMap<String, String> getRequestHeadParams() {
        return requestHeadParams;
    }

    public String getRequestLine() {
        return requestLine;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public String getRequestModel() {
        return requestModel;
    }

    public String getRequestMsg() {
        return requestMsg;
    }

    public HashMap<String, String> getRequestParams() {
        return requestParams;
    }

    public String getRequestURI() {
        return requestURI;
    }
}